package com.example.mysql;

import cn.hutool.core.util.RandomUtil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author csp 2020-12-21
 */
public class MyConfig {

    private String url = "jdbc:mysql://192.168.0.103:3306/mysql_plus?useSSL=false&serverTimezone=Asia/Shanghai&useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=utf-8";
    private String username = "qingyan";
    private String password = "qingyan!@#";
    private String forName = "com.mysql.jdbc.Driver";
    private Connection con = null;

    public MyConfig() {
        this.loadDriver();
        this.openLik();
    }

    private void loadDriver() {
        try {
            // 加载MySql的驱动类
            Class.forName(forName);
            System.out.println("MySql驱动加载成功");
        } catch (ClassNotFoundException e) {
            System.out.println("找不到驱动程序类 ，加载驱动失败！");
            e.printStackTrace();
        }

    }

    public void openLik() {
        try {
            System.out.println(url + username + password);
            con = DriverManager.getConnection(url, username, password);
            System.out.println("数据库连接成功！");
        } catch (SQLException se) {
            System.out.println("数据库连接失败！");
            se.printStackTrace();
        }
    }

    public Statement getSqlStringCommand(String query) {
        // TODO Auto-generated method stub
        PreparedStatement pstmt = null;
        try {
            pstmt = con.prepareStatement(query);
//            System.out.println("获取命令成功");
        } catch (Exception ex) {
            System.out.println("获取命令失败");
            ex.printStackTrace();
        }
        return pstmt;
    }

    public ResultSet executeDataSet(Statement cmd) {
        // TODO Auto-generated method stub
        ResultSet RS = null;
        PreparedStatement cmd_ = (PreparedStatement)cmd;
        try {
            cmd_.executeLargeUpdate();
//            System.out.println("获取ResultSet成功");
        } catch (Exception ex) {
            System.out.println("获取ResultSet失败");
            ex.printStackTrace();
        }
        return RS;
    }

    private void insertUserView() {

        StringBuffer sb = new StringBuffer("INSERT into t_user_view(user_id,viewed_user_id,viewed_user_sex,viewed_user_age) VALUES");
        for (int i = 0; i < 1000; i++) {
            int userId = RandomUtil.randomInt(10000);
            int viewedUserId = RandomUtil.randomInt(1000000);
            int viewedUserAge = RandomUtil.randomInt(60);
            int viewedUserSex = RandomUtil.randomInt(2);
            sb.append(String.format("(%d,%d,%d,%d),", userId, viewedUserId, viewedUserSex, viewedUserAge));
        }
        sb.deleteCharAt(sb.length() - 1).append(";");
        String format = sb.toString();
        System.out.println(format);
        Statement command = this.getSqlStringCommand(format);
        this.executeDataSet(command);
    }
    // 堆排序
    public static void main(String[] args){
        MyConfig config = new MyConfig();
        ExecutorService service = Executors.newFixedThreadPool(100);
        for (int i = 0; i < 5000; i++) {
            service.execute((()->config.insertUserView()));
        }
    }


}
